<?php
namespace Pub\wx;
use Pub\tool\Tool;
class Pay extends Wx
{
	private $_account = '';  //商户账号
	private $_key = '';  //商户密钥
	protected $_cert_cert = '';   //商户证书cert
	protected $_cert_key = '';  //商户证书key
	private $_order_id = '';  //订单id
	private $_prepay_id = '';  //微信下单返回prepay_id

	public function __construct($_order_id = '')
	{
		global $_W;
		parent::__construct();
		$_account = pdo_get('kuyuan_vbz_setting',array('uniacid'=>$_W['account']['uniacid']),array('mcht_account','mcht_key'));
		$this->_account = $_account['mcht_account'];
		$this->_key = $_account['mcht_key'];
		$this->_cert_cert = ATTACHMENT_ROOT.'/kuyuan_vbz/'.$_W['account']['uniacid'].'/wx_cert/cert.pem';
		$this->_cert_key = ATTACHMENT_ROOT.'/kuyuan_vbz/'.$_W['account']['uniacid'].'/wx_cert/key.pem';
		$this->_order_id = $_order_id;
	}

	/**
	 * 微信统一下单
	 */
	public function unifiedorder($_openid,$_money,$_body)
	{
		global $_W;
		$_data['appid'] = $this->_appid;
		$_data['mch_id'] = $this->_account;
		$_data['nonce_str'] = Tool::createCode();
		$_data['body'] = $_body;
		$_data['out_trade_no'] = $this->_order_id;
		$_data['total_fee'] = $_money * 100;
		$_data['spbill_create_ip'] = Tool::getIp();
		$_data['notify_url'] = $_W['siteroot'].$_W['script_name'];
		$_data['trade_type'] = 'JSAPI';
		$_data['openid'] = $_openid;
		$_data['sign'] = $this->sign($_data);
		$_xml = \LSS\Array2XML::createXML('xml',$_data)->saveXML();
		$_return = $this->wxHttpsRequestPem('https://api.mch.weixin.qq.com/pay/unifiedorder',$_xml);
		$_return = \LSS\XML2Array::createArray($_return);
		$_return = $_return['xml'];
		if($_return['result_code']['@cdata'] == 'SUCCESS' && $_return['return_code']['@cdata'] == 'SUCCESS'){
			$this->_prepay_id = $_return['prepay_id']['@cdata'];
			return $this->_prepay_id;
		}else{
			return false;
		}
	}

	/**
	 * JS支付，小程序支付获取支付参数
	 * @param unknown prepay_id
	 * @return string|unknown
	 */
	public function payParam(){
		$_data['appId'] = $this->_appid;
		$_data['timeStamp'] = ''.LOUIE_TIME;
		$_data['nonceStr'] = Tool::createCode();
		$_data['package'] = 'prepay_id='.$this->_prepay_id;
		$_data['signType'] = 'MD5';
		$_data['paySign'] = $this->sign($_data);
		return $_data;
	}

	/**
	 * 订单退款
	 * @param $_refundOrder  退款的订单号
	 * @param $_order_fee   订单金额
	 * @param $_refund_fee  退款金额
	 * @param string $_description  说明
	 * @throws Exception
	 */
	public function refund($_refundOrder,$_order_fee,$_refund_fee,$_description='')
	{
		$_data['appid'] = $this->_appid;
		$_data['mch_id'] = $this->_account;
		$_data['nonce_str'] = Tool::createCode();
		$_data['out_trade_no'] = $_refundOrder;
		$_data['out_refund_no'] = $this->_order_id;
		$_data['total_fee'] = $_order_fee*100;
		$_data['refund_fee'] = $_refund_fee*100;
		if(!empty($_description)) $_data['refund_desc'] = $_description;
		$_data['sign'] = $this->sign($_data);
		$_xml = \LSS\Array2XML::createXML('xml',$_data)->saveXML();
		$_return = $this->wxHttpsRequestPem('https://api.mch.weixin.qq.com/secapi/pay/refund',$_xml,true);
		$_return = \LSS\XML2Array::createArray($_return);
		$_return = $_return['xml'];
		if($_return['result_code']['@cdata'] == 'SUCCESS' && $_return['return_code']['@cdata'] == 'SUCCESS'){
			return array('status'=>'success');
		}else{
			$_err_msg = ($_return['err_code_des']['@cdata']) ? $_return['err_code_des']['@cdata'] : $_return['return_msg']['@cdata'];
			return array('status'=>'error','err_msg'=>$_err_msg);
		}
	}

	/**
	 * 企业付款给用户
	 * @param $_openid 付款给用户的openid
	 * @param $_money  付款金额
	 * @param $_remark   备注
	 * @return bool|DOMDocument|mixed  返回企业付款微信订单号
	 * @throws Exception
	 */
	public function payer($_openid,$_money,$_remark)
	{
		$_data['mch_appid'] = $this->_appid;
		$_data['mchid'] = $this->_account;
		$_data['nonce_str'] = Tool::createCode();
		$_data['partner_trade_no'] = $this->_order_id;
		$_data['openid'] = $_openid;
		$_data['check_name'] = 'NO_CHECK';
		$_data['amount'] = $_money*100;
		$_data['desc'] = $_remark;
		$_data['spbill_create_ip'] = Tool::getIp();
		$_data['sign'] = $this->sign($_data);
		$_xml = \LSS\Array2XML::createXML('xml',$_data)->saveXML();
		$_return = $this->wxHttpsRequestPem('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',$_xml,true);
		$_return = \LSS\XML2Array::createArray($_return);
		$_return = $_return['xml'];
		if($_return['result_code']['@cdata'] == 'SUCCESS' && $_return['return_code']['@cdata'] == 'SUCCESS'){
			return array('status'=>'success','data'=>$_return['payment_no']['@cdata']);
		}else{
			$_err_msg = ($_return['err_code_des']['@cdata']) ? $_return['err_code_des']['@cdata'] : $_return['return_msg']['@cdata'];
			return array('status'=>'error','err_msg'=>$_err_msg);
		}
	}

	/**
	 * 获取sign
	 * @param unknown $_data
	 * @return string
	 */
	private function sign($_data){
		ksort($_data);
		$_stringA = '';
		foreach($_data as $_key=>$_value){
			$_stringA .= $_key.'='.$_value.'&';
		}
		$_string = $_stringA.'key='.$this->_key;
		return strtoupper(md5($_string));
	}

	public function __set($name, $value)
	{
		$this->$name = $value;
	}
}